package com.tjetc.dao.impl;

import com.tjetc.dao.OrderDao;
import com.tjetc.pojo.dto.CheckInDTO;
import com.tjetc.pojo.entity.Order;
import com.tjetc.pojo.vo.OrderVO;
import com.tjetc.utils.DruidUtils;
import org.apache.commons.dbutils.handlers.BeanListHandler;

import java.time.LocalDateTime;
import java.util.List;

/**
 * @Author: itda
 * @Date: 2024/5/11 14:29
 * @Description: OrderDaoImpl
 */
public class OrderDaoImpl implements OrderDao {
    @Override
    public void insert(Order order) throws Exception {
        DruidUtils.r1.update(DruidUtils.getConnection(), "insert into orders (room_id, guest_id, check_in_date, check_out_date, total_price,status) " +
                "values (?,?,?,?,?,?)", order.getRoomId(), order.getGuestId(), order.getCheckInDate(), order.getCheckOutDate(), order.getTotalPrice(), order.getStatus());
    }

    @Override
    public void delete(Long id) throws Exception {
        DruidUtils.r1.update(DruidUtils.getConnection(), "delete from orders where id = ?", id);
    }

    @Override
    public void update(Order order) throws Exception {
        DruidUtils.r1.update(DruidUtils.getConnection(), "update orders set room_id = ?,guest_id = ?,check_in_date = ?,check_out_date = ?,total_price = ? where id = ?"
                , order.getRoomId(), order.getGuestId(), order.getCheckInDate(), order.getCheckOutDate(), order.getTotalPrice(), order.getId());
    }

    @Override
    public List<Order> list() throws Exception {
        return DruidUtils.r2.query("select * from orders", new BeanListHandler<>(Order.class, DruidUtils.rowProcessor));
    }

    @Override
    public List<OrderVO> getOrderByTime(LocalDateTime start, LocalDateTime end) throws Exception {
        return DruidUtils.r2.query("select o.room_id,o.guest_id,u.username,u.email,u.phone" +
                        ",o.check_in_date,o.check_out_date,o.total_price,o.status from orders o,user u,guest g where o.guest_id = g.id " +
                        "and g.user_id = u.id and o.check_out_date between ? and ?",
                new BeanListHandler<>(OrderVO.class, DruidUtils.rowProcessor), start, end);
    }

    @Override
    public List<Order> getByUHId(Long currentId, Long hotelId) throws Exception {
        return DruidUtils.r2.query("select o.* from orders o,guest g,user u,hotel h,room r where o.guest_id = g.id and g.user_id = ? and " +
                "o.room_id = r.id and ? = r.hotel_id",new BeanListHandler<>(Order.class),currentId,hotelId);
    }

    @Override
    public List<Order> getUserId(Long userId) throws Exception {
        return DruidUtils.r2.query("select distinct o.* from orders o,user u,guest g where g.user_id =? and o.guest_id = g.id",
                new BeanListHandler<>(Order.class,DruidUtils.rowProcessor),userId);
    }
}
